package org.java.xianxin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.java.xianxin.service.LogExceptionService;
import org.java.xianxin.web.Response;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.ResourceAccessException;

import java.net.ConnectException;
import java.net.URISyntaxException;
import java.nio.file.AccessDeniedException;
import java.text.ParseException;
import java.util.List;

@Slf4j
@Service
public class LogExceptionServiceImpl implements LogExceptionService {

    @Override
    public Response<Object> handle(Exception e) {
        log.error("异常拦截：", e);
        log.info("异常拦截：", e);
        if (e instanceof MethodArgumentNotValidException) {
            BindingResult bindingResult = ((MethodArgumentNotValidException) e).getBindingResult();
            List<ObjectError> ls = bindingResult.getAllErrors();
            final String[] msg = {"参数校验失败"};
            if (ls.size() == 1) {
                msg[0] = "";
                msg[0] = ls.get(0).getDefaultMessage();
            }
            if (ls.size() > 1) {
                msg[0] = "";
                ls.forEach(objectError -> {
                    msg[0] += objectError.getDefaultMessage() + " ";
                });
            }
            return Response.fail(msg[0]);
        }

        if (e instanceof ArithmeticException) {
            return Response.fail(e.getMessage());
        }

        if (e instanceof HttpRequestMethodNotSupportedException) {
            return Response.fail(e.getMessage());
        }

        if (e instanceof HttpServerErrorException) {
            if (e.getMessage().contains("500 Internal Server Error")) {
                return Response.fail(500, "内部服务器错误");
            }
        }

        if (e instanceof ConnectException) {
            return Response.fail("远程连接被拒绝");
        }

        if (e instanceof NullPointerException) {
            return Response.fail("服务内部空指针异常");
        }

        if (e instanceof HttpMessageNotWritableException) {
            return Response.fail("响应数据解析异常");
        }

        if (e instanceof IllegalArgumentException) {
            return Response.fail("坏的请求");
        }

        if (e instanceof AccessDeniedException) {
            return Response.fail("拒绝访问异常");
        }

        if (e instanceof HttpMessageNotReadableException) {
            return Response.fail("请求参数解析出错");
        }

        if (e instanceof ResourceAccessException) {
            return Response.fail(e.getMessage());
        }

        if (e instanceof URISyntaxException) {
            return Response.fail("URL解析失败");
        }

        if (e instanceof ParseException) {

            return Response.fail(e.getMessage());
        }

        return Response.fail("糟糕，系统好像走神了~");
    }
}
